﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Threading;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace 药品信息管理
{
    public partial class MainForm : DevComponents.DotNetBar.Office2007RibbonForm
    {
        //Thread thread1; //创建线程thread1

        public MainForm()
        {
            //thread1 = new Thread(t1); //初始化线程thread1并使用自定义方法t1      

            InitializeComponent();
        }
        #region 子窗只能打开一个
        private bool checkChildFrmExist(string childFrmName)
        {
            foreach (Form childFrm in this.MdiChildren)
            {
                //用子窗体的Name进行判断，如果已经存在则将他激活
                if (childFrm.Name == childFrmName)
                {
                    if (childFrm.WindowState == FormWindowState.Minimized)
                        childFrm.WindowState = FormWindowState.Maximized;
                    childFrm.Activate();
                    return true;
                }
            }
            return false;
        }
        #endregion

        private void MainForm_Load(object sender, EventArgs e)
        {
            eStyle style = (eStyle)Enum.Parse(typeof(eStyle), "Windows7Blue");
            StyleManager.ChangeStyle(style, Color.Empty);
            monthCalendarAdv2.DisplayMonth = DateTime.Today;
            labelItem1.Text = "当前使用人员：" + LoginForm.username;

            slidePanel1.Size = new System.Drawing.Size(this.Size.Width, this.Size.Height - ribbonControl1.Size.Height - tabStrip1.Size.Height - bar1.Size.Height - 3);

            try
            {
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    SqlDataAdapter da = new SqlDataAdapter();//实例化sqldataadpte
                    DataTable dt_gqyp = new DataTable();
                    SqlCommand gqyp = new SqlCommand("SELECT BM 编码,MC 名称,SL 数量,(select convert(varchar(10),(select convert(datetime,YXRQ-2)),120)) as 有效日期 FROM YK_RKD1 LEFT JOIN ZD_YP ON YPBM=BM  WHERE BM LIKE'%%' AND (select convert(varchar(10),(select convert(datetime,YXRQ-2)),120))<=(select convert(varchar(10),getdate(),120))", con);//sql语句
                    da.SelectCommand = gqyp;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt_gqyp);
                    dgv_gq.DataSource = dt_gqyp;
                    labelX3.Text = labelX3.Text.ToString() + dt_gqyp.Rows.Count.ToString();


                    DataTable dt_kcyj = new DataTable();
                    SqlCommand sql_kcyj = new SqlCommand("SELECT ZD_YP.BM 编码,MC 名称,SX 上限,XX 下限,KCSL 库存数量 FROM ZD_YP,(SELECT YPBM ,SUM(SL) AS KCSL FROM YP_CRKJZ GROUP BY YPBM) YPKC WHERE ZD_YP.BM=YPBM AND BM LIKE'%%'  AND (KCSL>SX OR  KCSL<XX)", con);//sql语句
                    da.SelectCommand = sql_kcyj;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt_kcyj);
                    dgv_yj.DataSource = dt_kcyj;
                    labelX4.Text = labelX4.Text.ToString() + dt_kcyj.Rows.Count.ToString();


                }
            }
            catch { }
            //for (int i = 0; i < 9; i++)
            //    MessageBox.Show(i.ToString() + ribbonControl1.Items[i].Text.ToString());
            if (LoginForm.qx != "all")//是否为超级权限，否则分配权限
            {
                string[] qxArray = Regex.Split(LoginForm.qx, ",", RegexOptions.IgnoreCase);

                //初始化主菜单
                int[] cd = new int[9] { 0, 0, 0, 0, 0, 0,1,1,1 };
                //初始化从菜单
                int[] xs_xxgl = new int[6]{0,0,0,0,0,0};
                int[] xs_kcgl = new int[4]{0,0,0,0};
                int[] xs_zwcl = new int[4] { 0, 0, 0, 0 };
                int[] xs_cxytj = new int[6] { 0, 0, 0, 0, 0, 0 };
                int[] xs_xtwh = new int[2] { 0, 0};
                int[] xs_xtsz = new int[4] { 0, 0, 0, 0 };


                #region 分配权限
                for (int i = 0; i < qxArray.Length; i++)
                {
                    //分配药品信息管理权限
                    for (int j = 0; j < rb_xxgl.Items.Count; j++)
                    {
                        if (rb_xxgl.Items[j].Name == qxArray[i])
                        {
                            rb_xxgl.Items[j].Visible = true;
                            ypxxgl.Visible = true;
                            ypxxgl.Checked = true;
                            xs_xxgl[j] = 1;
                            cd[0] = 1;

                        }

                    }
                    //分配库存管理权限
                    for (int k = 0; k < rb_kcgl.Items.Count; k++)
                    {
                        if (rb_kcgl.Items[k].Name == qxArray[i])
                        {
                            rb_kcgl.Items[k].Visible = true;
                            ypkcgl.Visible = true;
                            ypkcgl.Checked = true;
                            xs_kcgl[k] = 1;
                            cd[1] = 1;

                        }

                    }
                    //分配账务处理权限
                    for (int l = 0; l < rb_zwcl.Items.Count; l++)
                    {
                        if (rb_zwcl.Items[l].Name == qxArray[i])
                        {
                            rb_zwcl.Items[l].Visible = true;
                            zwcl.Visible = true;
                            zwcl.Checked = true;
                            xs_zwcl[l] = 1;
                            cd[2] = 1;

                        }

                    }
                    //分配查询与统计权限
                    for (int m = 0; m < rb_cxtj.Items.Count; m++)
                    {
                        if (rb_cxtj.Items[m].Name == qxArray[i])
                        {
                            rb_cxtj.Items[m].Visible = true;
                            cxytj.Visible = true;
                            cxytj.Checked = true;
                            xs_cxytj[m] = 1;
                            cd[3] = 1;

                        }

                    }
                    //分配系统维护权限
                    for (int n = 0; n < rb_xtwh.Items.Count; n++)
                    {
                        if (rb_xtwh.Items[n].Name == qxArray[i])
                        {
                            rb_xtwh.Items[n].Visible = true;
                            xtwh.Visible = true;
                            xtwh.Checked = true;
                            xs_xtwh[n] = 1;
                            cd[4] = 1;

                        }

                    }
                    //分配系统设置权限
                    for (int o = 0; o < rb_xtsz.Items.Count; o++)
                    {
                        if (rb_xtsz.Items[o].Name == qxArray[i])
                        {
                            rb_xtsz.Items[o].Visible = true;
                            xtsz.Visible = true;
                            xtsz.Checked = true;
                            xs_xtsz[o] = 1;
                            cd[5] = 1;

                        }

                    }
                }
                #endregion

                //主菜单 无权限 不可见
                for (int i = 0; i < ribbonControl1.Items.Count; i++)
                {
                    if (cd[i] == 0)
                        ribbonControl1.Items[i].Visible = false;
                }
                //从菜单无权限 不可见
                for (int i = 0; i < rb_xxgl.Items.Count; i++)
                {
                    if (xs_xxgl[i] != 1)
                        rb_xxgl.Items[i].Visible = false;
                }
                for (int i = 0; i < rb_kcgl.Items.Count; i++)
                {
                    if (xs_kcgl[i] != 1)
                        rb_kcgl.Items[i].Visible = false;
                }
                for (int i = 0; i < rb_zwcl.Items.Count; i++)
                {
                    if (xs_zwcl[i] != 1)
                        rb_zwcl.Items[i].Visible = false;
                }
                for (int i = 0; i < rb_cxtj.Items.Count; i++)
                {
                    if (xs_cxytj[i] != 1)
                        rb_cxtj.Items[i].Visible = false;
                }
                for (int i = 0; i < rb_xtwh.Items.Count; i++)
                {
                    if (xs_xtwh[i] != 1)
                        rb_xtwh.Items[i].Visible = false;
                }
                for (int i = 0; i < rb_xtsz.Items.Count; i++)
                {
                    if (xs_xtsz[i] != 1)
                        rb_xtsz.Items[i].Visible = false;
                }
            }
                    
        }

        private void Swith_Executed(object sender, EventArgs e)
        {
            ribbonControl1.Expanded = Swith.Checked;
            Swith.Checked = !Swith.Checked;
        }

        private void AppCommandTheme_Executed(object sender, EventArgs e)
        {
            ICommandSource source = sender as ICommandSource;
            if (source.CommandParameter is string)
            {
                eStyle style = (eStyle)Enum.Parse(typeof(eStyle), source.CommandParameter.ToString());
                // Using StyleManager change the style and color tinting
                StyleManager.ChangeStyle(style, Color.Empty);
                if (style == eStyle.Office2007Black || style == eStyle.Office2007Blue || style == eStyle.Office2007Silver || style == eStyle.Office2007VistaGlass)
                    buttonFile.BackstageTabEnabled = false;
                else
                    buttonFile.BackstageTabEnabled = true;

            }
            else if (source.CommandParameter is Color)
            {
                StyleManager.ColorTint = (Color)source.CommandParameter;
            }
        }

        private void buttonStyleCustom_ColorPreview(object sender, ColorPreviewEventArgs e)
        {
            StyleManager.ColorTint = e.Color;
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            
            //backgroundWorker1.RunWorkerAsync();
            if (this.checkChildFrmExist("YP_ZDWH") == true)
            {
                return;
            }
            YP_ZDWH ZDWH = new YP_ZDWH();
            ZDWH.MdiParent = this;
            ZDWH.WindowState = FormWindowState.Maximized;
            ZDWH.Show();

        }
        

        private void buttonItem5_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_JBGL") == true)
            {
                return;
            }
            YP_JBGL JBGL = new YP_JBGL();
            JBGL.MdiParent = this;
            JBGL.WindowState = FormWindowState.Maximized;
            JBGL.Show();
        }


        private void buttonItem6_Click_1(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_LJTZ") == true)
            {
                return;
            }
            YP_LJTZ LJTZ = new YP_LJTZ();
            LJTZ.MdiParent = this;
            LJTZ.WindowState = FormWindowState.Maximized;
            LJTZ.Show();
        }



        private void buttonItem3_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_KCYJ") == true)
            {
                return;
            }
            YP_KCYJ KCYJ = new YP_KCYJ();
            KCYJ.MdiParent = this;
            KCYJ.WindowState = FormWindowState.Maximized;
            KCYJ.Show();
        }

        private void buttonItem4_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_YXQCX") == true)
            {
                return;
            }
            YP_YXQCX YXQCX = new YP_YXQCX();
            YXQCX.MdiParent = this;
            YXQCX.WindowState = FormWindowState.Maximized;
            YXQCX.Show();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
           
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
           // circularProgress1.Value += 2;
        }

        private void buttonItem12_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_JXGL") == true)
            {
                return;
            }
            YP_JXGL JXGL = new YP_JXGL();
            JXGL.MdiParent = this;
            JXGL.WindowState = FormWindowState.Maximized;
            JXGL.Show();
        }

        private void buttonItem8_Click_1(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("药品入库") == true)
            {
                return;
            }
            药品入库 入库 = new 药品入库();
            入库.MdiParent = this;
            入库.WindowState = FormWindowState.Maximized;
            入库.Show();
        }

        private void buttonItem10_Click_1(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("药品出库") == true)
            {
                return;
            }
            药品出库 出库 = new 药品出库();
            出库.MdiParent = this;
            出库.WindowState = FormWindowState.Maximized;
            出库.Show();
        }

        private void buttonItem11_Click_1(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("药品盘点") == true)
            {
                return;
            }
            药品盘点 盘点 = new 药品盘点();
            盘点.MdiParent = this;
            盘点.WindowState = FormWindowState.Maximized;
            盘点.Show();
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            foreach (Form childFrm in this.MdiChildren)
            {
               childFrm.Close();
            }
        }


        private void labelX2_Click_1(object sender, EventArgs e)
        {
            slidePanel1.Size = new System.Drawing.Size(this.Size.Width,this.Size.Height-ribbonControl1.Size.Height-tabStrip1.Size.Height-bar1.Size.Height-3);

        }

        private void buttonX1_Click_1(object sender, EventArgs e)
        {
            slidePanel1.IsOpen = false;

        }

        private void ypljtzjcz_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_LJTZCZ") == true)
            {
                return;
            }
            YP_LJTZCZ LJTZCZ = new YP_LJTZCZ();
            LJTZCZ.MdiParent = this;
            LJTZCZ.WindowState = FormWindowState.Maximized;
            LJTZCZ.Show();
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void zczr_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("药品直入直出") == true)
            {
                return;
            }
            药品直入直出 直入直出 = new 药品直入直出();
            直入直出.MdiParent = this;
            直入直出.WindowState = FormWindowState.Maximized;
            直入直出.Show();
        }

        private void rygl_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("RYGL") == true)
            {
                return;
            }
            RYGL RY = new RYGL();
            RY.MdiParent = this;
            RY.WindowState = FormWindowState.Maximized;
            RY.Show();
        }

        private void qxgl_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("QXGLForm") == true)
            {
                return;
            }
            QXGLForm QX = new QXGLForm();
            QX.MdiParent = this;
            QX.WindowState = FormWindowState.Maximized;
            QX.Show();
        }

        private void buttonItem11_Click(object sender, EventArgs e)
        {
            this.Dispose();
            LoginForm newlogin = new LoginForm();
            newlogin.Show();
        }

        private void zkdcx_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_RKDCX") == true)
            {
                return;
            }
            YP_RKDCX RKDCX = new YP_RKDCX();
            RKDCX.MdiParent = this;
            RKDCX.WindowState = FormWindowState.Maximized;
            RKDCX.Show();
        }

        private void ckdcx_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_CKDCX") == true)
            {
                return;
            }
            YP_CKDCX CKDCX = new YP_CKDCX();
            CKDCX.MdiParent = this;
            CKDCX.WindowState = FormWindowState.Maximized;
            CKDCX.Show();
        }

        private void yfqldcx_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_QLDCX") == true)
            {
                return;
            }
            YP_QLDCX QLDCX = new YP_QLDCX();
            QLDCX.MdiParent = this;
            QLDCX.WindowState = FormWindowState.Maximized;
            QLDCX.Show();
        }

        private void ypcrmx_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_CRKMX") == true)
            {
                return;
            }
            YP_CRKMX CRKMX = new YP_CRKMX();
            CRKMX.MdiParent = this;
            CRKMX.WindowState = FormWindowState.Maximized;
            CRKMX.Show();
        }

        private void ypxhpm_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_XHPM") == true)
            {
                return;
            }
            YP_XHPM XHPM = new YP_XHPM();
            XHPM.MdiParent = this;
            XHPM.WindowState = FormWindowState.Maximized;
            XHPM.Show();
        }

        private void ypzxcx_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_ZXCX") == true)
            {
                return;
            }
            YP_ZXCX ZXCX = new YP_ZXCX();
            ZXCX.MdiParent = this;
            ZXCX.WindowState = FormWindowState.Maximized;
            ZXCX.Show();
        }

        private void crklxdy_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_CRKLX") == true)
            {
                return;
            }
            YP_CRKLX CRKLX = new YP_CRKLX();
            CRKLX.MdiParent = this;
            CRKLX.WindowState = FormWindowState.Maximized;
            CRKLX.Show();
        }

        private void gysdy_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_GYS") == true)
            {
                return;
            }
            YP_GYS GYS = new YP_GYS();
            GYS.MdiParent = this;
            GYS.WindowState = FormWindowState.Maximized;
            GYS.Show();
        }

        private void yppzwh_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_PZWH") == true)
            {
                return;
            }
            YP_PZWH PZWH = new YP_PZWH();
            PZWH.MdiParent = this;
            PZWH.WindowState = FormWindowState.Maximized;
            PZWH.Show();
        }

        private void tbfp_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("tbfp") == true)
            {
                return;
            }
            TBFP fp = new TBFP();
            fp.MdiParent = this;
            fp.WindowState = FormWindowState.Maximized;
            fp.Show();
        }

        private void yfkcx_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YK_YFKCX") == true)
            {
                return;
            }
            YK_YFKCX YFKCX = new YK_YFKCX();
            YFKCX.MdiParent = this;
            YFKCX.WindowState = FormWindowState.Maximized;
            YFKCX.Show();
        }

        private void yprzpz_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("YP_RZPZ") == true)
            {
                return;
            }
            YP_RZPZ RZPZ = new YP_RZPZ();
            RZPZ.MdiParent = this;
            RZPZ.WindowState = FormWindowState.Maximized;
            RZPZ.Show();
        }

        private void ksdy_Click(object sender, EventArgs e)
        {
            if (this.checkChildFrmExist("ZD_KS") == true)
            {
                return;
            }
            ZD_KS KS = new ZD_KS();
            KS.MdiParent = this;
            KS.WindowState = FormWindowState.Maximized;
            KS.Show();
        }

    }
}